# Load Data
setwd("/users/josephphillips/Dropbox/COVID-19/Data/FC Data Repository")
df <- read.csv("USA.csv",header=T,sep=",")

# Load glmnet function
run_LASSO <- function(data, yvar, pre){
  
  set.seed(2334)
  
  var_quo <- enquo(yvar)
  var_quo1 <- enquo(pre)
  
  ## select x variables 
  dat <- data %>%
    dplyr::select(college, agegroup, male, married, frequent_church, region, pid3, ideo7, lives_highincidence,crt, knowledge,nonwhite, polinterest,health_trust,media_trust)
  
  pre <- data %>%
    dplyr::select(!!var_quo1) %>% pull() # Save as a vector instead of a data frame ot use model matrix
  
  ## select y-variable
  out1 <- data %>% 
    dplyr::select(!!var_quo) %>% pull() # save as a vector instead of a data-frame to use model.matrix()
  
  merged1 <- cbind(out1, dat, pre) %>% na.omit() ## merge and delete NA
  x1 <- model.matrix(out1 ~ ., data = merged1) ## make model matrix
  y1 <- merged1$out1 ## select y-variable from NA-deleted dataset
  mod1 <- cv.glmnet(x1, y1, alpha = 1, family = "gaussian") ## run lasso model
  coef <- coef(mod1) ## get coef  
  
  list(merged1, x1, y1, mod1, coef)
  
}

run_LASSO_nopre <- function(data, yvar){
  
  set.seed(2334)
  
  var_quo <- enquo(yvar)
  
  ## select x variables 
  dat <- data %>%
    dplyr::select(college, agegroup, male, married, frequent_church, region, pid3, ideo7, lives_highincidence,crt, knowledge,nonwhite, polinterest,health_trust,media_trust)

  ## select y-variable
  out1 <- data %>% 
    dplyr::select(!!var_quo) %>% pull() # save as a vector instead of a data-frame to use model.matrix()
  
  merged1 <- cbind(out1, dat) %>% na.omit() ## merge and delete NA
  x1 <- model.matrix(out1 ~ ., data = merged1) ## make model matrix
  y1 <- merged1$out1 ## select y-variable from NA-deleted dataset
  mod1 <- cv.glmnet(x1, y1, alpha = 1, family = "gaussian") ## run lasso model
  coef <- coef(mod1) ## get coef  
  
  list(merged1, x1, y1, mod1, coef)
  
}

# Lasso Regressions
## W1
W1_tfc <- run_LASSO_nopre(df,target_false) # college, frequent church, pid3, ideo7, lives_highincidence, crt, knowledge, nonwhite, healthtrust, media trust
W1_nfc <- run_LASSO_nopre(df,nontarget_false) # frequent church, pid3, ideo7, lives_highincidence, crt, knowledge, nonwhite, health_trust
W1_nfc_noleak <- run_LASSO_nopre(df,nontarget_false_noleak) # frequent church, ideo7, lives_highincidence, crt, knowledge, nonwhite, health_trust
W1_ntc <- run_LASSO_nopre(df,nontarget_true) # male, frequent church, pid3, ideo7, knowledge, polinterest, health trust, media trust

## H1A
h1a <- run_LASSO(df,W2_target_false,pre=target_false)[[5]] ## ideo7, health_trust, media_trust, pre
h1a.exp1 <- run_LASSO(df,W2_accuracy_con_theory1,pre=accuracy_con_theory1)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
h1a.exp2 <- run_LASSO(df,W2_accuracy_con_theory5,pre=accuracy_con_theory5)[[5]] ## health_trust, media_trust, pre
h1a.exp3 <- run_LASSO(df,W2_accuracy_False1,pre=accuracy_False1)[[5]] ## ideo7, lives_highincidence, crt, knowledge, pre
h1a.exp4 <- run_LASSO(df,W2_accuracy_False4,pre=accuracy_False4)[[5]] ## ideo7, health_trust, media_trust, pre

## H1b/H2/RQ1
### W3
h1b <- run_LASSO(df,W3_target_false,pre=target_false)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
h1b.exp1 <- run_LASSO(df,W3_accuracy_con_theory1,pre=accuracy_con_theory1)[[5]] ## pid3, ideo7, knowledge, health_trust, media_trust, pre
h1b.exp2 <- run_LASSO(df,W3_accuracy_con_theory5,pre=accuracy_con_theory5)[[5]] ## ideo7, knowledge, health_trust, media_trust, pre
h1b.exp3 <- run_LASSO(df,W3_accuracy_False1,pre=accuracy_False1)[[5]] ## ideo7, crt, knowledge, health_trust, pre
h1b.exp4 <- run_LASSO(df,W3_accuracy_False4,pre=accuracy_False4)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
### W4
h1b.w4 <- run_LASSO(df,W4_target_false,pre=target_false)[[5]] ## pid3, ideo7, crt, health_trust, media_trust, pre
h1b.exp1.w4 <- run_LASSO(df,W4_accuracy_con_theory1,pre=accuracy_con_theory1)[[5]] ## pid3, ideo7, crt, health_trust, media_trust, pre
h1b.exp2.w4 <- run_LASSO(df,W4_accuracy_con_theory5,pre=accuracy_con_theory5)[[5]] ## pid3, ideo7, knowledge, health_trust, media_trust, pre
h1b.exp3.w4 <- run_LASSO(df,W4_accuracy_False1,pre=accuracy_False1)[[5]] ## ideo7, crt, knowledge, pre
h1b.exp4.w4 <- run_LASSO(df,W4_accuracy_False4,pre=accuracy_False4)[[5]] ## pid3, ideo7, health_trust, media_trust, pre

## RQ2/RQ3
### W2
rq2.false.full.w2 <- run_LASSO(df,W2_nontarget_false,pre=nontarget_false)[[5]] ## ideo7, health_trust, pre
rq2.false.noleak.w2 <- run_LASSO(df,W2_nontarget_false_noleak,pre=nontarget_false_noleak)[[5]] ## pre
rq2.true.full.w2 <- run_LASSO(df,W2_nontarget_true,pre=nontarget_true)[[5]] ## ideo7, health_trust, pre
rq2.change2.w2 <- run_LASSO(df,W2_accuracy_change2,pre=accuracy_change2)[[5]] ## knowledge, pre
rq2.false2.w2 <- run_LASSO(df,W2_accuracy_False2,pre=accuracy_False2)[[5]] ## pre
rq2.false3.w2 <- run_LASSO(df,W2_accuracy_False3,pre=accuracy_False3)[[5]] ## pre
rq2.edge1.w2 <- run_LASSO(df,W2_accuracy_edge1,pre=accuracy_edge1)[[5]] ## knowledge, pre
rq2.con_theory2.w2 <- run_LASSO(df,W2_accuracy_con_theory2,pre=accuracy_con_theory2)[[5]] ## pid3, ideo7, health_trust, pre
rq2.fcure1.w2  <- run_LASSO(df,W2_accuracy_f_cure1,pre=accuracy_f_cure1)[[5]] ## pre
rq2.fcure2.w2  <- run_LASSO(df,W2_accuracy_f_cure2,pre=accuracy_f_cure2)[[5]] ## ideo7, crt, health_trust, pre
rq2.fcure3.w2  <- run_LASSO(df,W2_accuracy_f_cure3,pre=accuracy_f_cure3)[[5]] ## ideo7, pre
rq2.true1.w2 <- run_LASSO(df,W2_accuracy_True1,pre=accuracy_True1)[[5]] ## ideo7, knowledge, health_trust, pre
rq2.true2.w2 <- run_LASSO(df,W2_accuracy_True2,pre=accuracy_True2)[[5]] ## pre
rq2.true3.w2 <- run_LASSO(df,W2_accuracy_True3,pre=accuracy_True3)[[5]] ## health_trust, pre
rq2.true4.w2 <- run_LASSO(df,W2_accuracy_True4,pre=accuracy_True4)[[5]] ## ideo7, health_trust, pre
rq2.true5.w2 <- run_LASSO(df,W2_accuracy_True5,pre=accuracy_True5)[[5]] ## pre
rq2.true6.w2 <- run_LASSO(df,W2_accuracy_True6,pre=accuracy_True6)[[5]] ## ideo7, health_trust, pre
rq2.change3.w2 <- run_LASSO(df,W2_accuracy_change3,pre=accuracy_change3)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.change1.w2 <- run_LASSO(df,W2_accuracy_change1,pre=accuracy_change1)[[5]] ## ideo7, health_trust, pre
rq2.change4.w2 <- run_LASSO(df,W2_accuracy_change4,pre=accuracy_change4)[[5]] ## knowledge, polinterest, pre
### W3
rq2.false.full.w3 <- run_LASSO(df,W3_nontarget_false,pre=nontarget_false)[[5]] ## ideo7, health_trust, pre
rq2.false.noleak.w3 <- run_LASSO(df,W3_nontarget_false_noleak,pre=nontarget_false_noleak)[[5]] ## pre
rq2.true.full.w3 <- run_LASSO(df,W3_nontarget_true,pre=nontarget_true)[[5]] ## ideo7, health_trust, media_trust, pre
rq2.change2.w3 <- run_LASSO(df,W3_accuracy_change2,pre=accuracy_change2)[[5]] ## knowledge, pre
rq2.false2.w3 <- run_LASSO(df,W3_accuracy_False2,pre=accuracy_False2)[[5]] ## pre
rq2.false3.w3 <- run_LASSO(df,W3_accuracy_False3,pre=accuracy_False3)[[5]] ## pre
rq2.edge1.w3 <- run_LASSO(df,W3_accuracy_edge1,pre=accuracy_edge1)[[5]] ## pre
rq2.con_theory2.w3 <- run_LASSO(df,W3_accuracy_con_theory2,pre=accuracy_con_theory2)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.fcure1.w3  <- run_LASSO(df,W3_accuracy_f_cure1,pre=accuracy_f_cure1)[[5]] ## pre
rq2.fcure2.w3  <- run_LASSO(df,W3_accuracy_f_cure2,pre=accuracy_f_cure2)[[5]] ## ideo7, health_trust, pre
rq2.fcure3.w3  <- run_LASSO(df,W3_accuracy_f_cure3,pre=accuracy_f_cure3)[[5]] ## ideo7, knowledge, health_trust, pre
rq2.true1.w3 <- run_LASSO(df,W3_accuracy_True1,pre=accuracy_True1)[[5]] ## health_trust, pre
rq2.true2.w3 <- run_LASSO(df,W3_accuracy_True2,pre=accuracy_True2)[[5]] ## health_trust, pre
rq2.true3.w3 <- run_LASSO(df,W3_accuracy_True3,pre=accuracy_True3)[[5]] ## ideo7, health_trust, pre
rq2.true4.w3 <- run_LASSO(df,W3_accuracy_True4,pre=accuracy_True4)[[5]] ## ideo7, health_trust, pre
rq2.true5.w3 <- run_LASSO(df,W3_accuracy_True5,pre=accuracy_True5)[[5]] ## pre
rq2.true6.w3 <- run_LASSO(df,W3_accuracy_True6,pre=accuracy_True6)[[5]] ## ideo7, knowledge, health_trust, pre
rq2.change3.w3 <- run_LASSO(df,W3_accuracy_change3,pre=accuracy_change3)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.change1.w3 <- run_LASSO(df,W3_accuracy_change1,pre=accuracy_change1)[[5]] ## ideo7, knowledge, health_trust, media_trust, pre
rq2.change4.w3 <- run_LASSO(df,W3_accuracy_change4,pre=accuracy_change4)[[5]] ## knowledge, polinterest, media_trust, pre
### W4
rq2.false.full.w4 <- run_LASSO(df,W4_nontarget_false,pre=nontarget_false)[[5]] ## ideo7, health_trust, pre
rq2.false.noleak.w4 <- run_LASSO(df,W4_nontarget_false_noleak,pre=nontarget_false_noleak)[[5]] ## ideo7, pre
rq2.true.full.w4 <- run_LASSO(df,W4_nontarget_true,pre=nontarget_true)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.change2.w4 <- run_LASSO(df,W4_accuracy_change2,pre=accuracy_change2)[[5]] ## pid3, ideo7, knowledge, health_trust
rq2.false2.w4 <- run_LASSO(df,W4_accuracy_False2,pre=accuracy_False2)[[5]] ## pre
rq2.false3.w4 <- run_LASSO(df,W4_accuracy_False3,pre=accuracy_False3)[[5]] ## pre
rq2.edge1.w4 <- run_LASSO(df,W4_accuracy_edge1,pre=accuracy_edge1)[[5]] ## pre
rq2.con_theory2.w4 <- run_LASSO(df,W4_accuracy_con_theory2,pre=accuracy_con_theory2)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.fcure1.w4  <- run_LASSO(df,W4_accuracy_f_cure1,pre=accuracy_f_cure1)[[5]] ## pre
rq2.fcure2.w4  <- run_LASSO(df,W4_accuracy_f_cure2,pre=accuracy_f_cure2)[[5]] ## pid3, ideo7, health_trust, pre
rq2.fcure3.w4  <- run_LASSO(df,W4_accuracy_f_cure3,pre=accuracy_f_cure3)[[5]] ## ideo7, knowledge, health_trust, pre
rq2.true1.w4 <- run_LASSO(df,W4_accuracy_True1,pre=accuracy_True1)[[5]] ## ideo7, knowledge, health_trust, pre
rq2.true2.w4 <- run_LASSO(df,W4_accuracy_True2,pre=accuracy_True2)[[5]] ## health_trust, media_trust, pre
rq2.true3.w4 <- run_LASSO(df,W4_accuracy_True3,pre=accuracy_True3)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.true4.w4 <- run_LASSO(df,W4_accuracy_True4,pre=accuracy_True4)[[5]] ## ideo7, health_trust, pre
rq2.true5.w4 <- run_LASSO(df,W4_accuracy_True5,pre=accuracy_True5)[[5]] ## pre
rq2.true6.w4 <- run_LASSO(df,W4_accuracy_True6,pre=accuracy_True6)[[5]] ## ideo7, crt, knolwedge, health_trust, pre
rq2.change3.w4 <- run_LASSO(df,W4_accuracy_change3,pre=accuracy_change3)[[5]] ## pid3, ideo7, health_trust, media_trust, pre
rq2.change1.w4 <- run_LASSO(df,W4_accuracy_change1,pre=accuracy_change1)[[5]] ## ideo7, knowledge, polinterest, health_trust, pre
rq2.change4.w4 <- run_LASSO(df,W4_accuracy_change4,pre=accuracy_change4)[[5]] ## knowledge, polinterest, pre